package servlet;

import bean.InboundBean;
import com.alibaba.fastjson.JSON;
import util.JdbcUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class InboundHuntServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        //获取传进来的
        String seeksupplierin = request.getParameter("seeksupplierin");
        String seekrecordin  =	request.getParameter("seekrecordin");
        //获取模糊查询的数据
        System.out.println("进来了查询界面");
        String sql="select * from supplier_in a join record_in b on a.inid=b.inid where a.name like ? or b.rname like ?";
        try {
            PreparedStatement ps = JdbcUtil.getConnection().prepareStatement(sql);
            ps.setString(1, "%"+seeksupplierin+"%");
            ps.setString(2, "%"+seekrecordin+"%");
            ResultSet set=ps.executeQuery();
            ArrayList<InboundBean> arr = new ArrayList<InboundBean>();
            while (set.next()) {
                InboundBean in = new InboundBean();
                in.setSinid(set.getInt(1));
                in.setName(set.getString(2));
                in.setPerson(set.getString(3));
                in.setInid(set.getInt(4));
                in.setEmail(set.getString(5));
                in.setTel(set.getString(6));
                in.setAddress(set.getString(7));
                in.setWid(set.getInt(9));
                in.setType(set.getString(10));
                in.setRname(set.getString(11));
                in.setPrice(set.getDouble(12));
                in.setNumber(set.getDouble(13));
                in.setTime(set.getString(14));
                arr.add(in);
            }
            System.out.println(arr.toString());
            String json = JSON.toJSONString(arr);
            out.print(json);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
}
